package com.yiyuan.km.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.common.exception.SubRecExistException;
import com.yiyuan.km.model.KmType;
import com.yiyuan.km.service.IKmService;
import com.yiyuan.system.object.TreeObj;

public class KmTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	// 参数
	private String typeId;
	private String typeName;
	private String typePid;
	private String id;//分类树查询下级节点

	private String operation;

	private KmType kmType;

	// 资源引用
	@Resource
	private IKmService kmService;

	/** *****************分类操作******************************* */
	// 进入分类管理主页
	public String index() {
		return "toMain";
	}

	// 获得分类树
	@SuppressWarnings("unchecked")
	public void list() {
		TreeObj treeObj = null;
		KmType kmType = null;
		List<TreeObj> treeList = new ArrayList();
		if (this.id == null || this.id.equals("")) {
			this.id = "0";
		}
		List<KmType> kmTypeList = kmService.findSubTypeList(this.id);
		for (int i = 0; i < kmTypeList.size(); i++) {
			kmType = kmTypeList.get(i);
			treeObj = new TreeObj();
			treeObj.setId(kmType.getTypeId().toString());
			treeObj.setPid(kmType.getTypePid().toString());
			treeObj.setText(kmType.getTypeName());
			treeObj.setCheckstate(0);
			treeObj.setChildNodes(null);
			treeObj.setComplete(false);
			if (kmType.getHasChild() != null
					&& kmType.getHasChild().equals("1")) {
				treeObj.setHasChildren(true);
			} else {
				treeObj.setHasChildren(false);
			}
			treeObj.setIsexpand(false);
			treeObj.setShowcheck(true);
			treeObj.setValue(kmType.getTypeId().toString());
			treeList.add(treeObj);

		}
		Map map = new HashMap();
		map.put("list", treeList);
		JSONObject jso = new JSONObject(map);
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter()
					.print(jso.toString());
		} catch (Exception e) {
		}
	}

	// 保存知识分类
	public void save() {
		KmType kmType = null;
		String retFlag = null;
		try {
			if (this.typeId == null || this.typeId.equals("")) {// 新增
				kmType = new KmType();
				kmType.setTypeName(this.typeName);
				kmType.setTypePid(Integer.valueOf(this.getTypePid()));
				kmType.setHasChild("0");
				kmService.addType(kmType);
				retFlag = "add";
			} else {// 修改
				kmType = this.kmService.getType(Integer.valueOf(this.typeId));
				kmType.setTypeName(this.typeName);
				this.kmService.updateType(kmType);
				retFlag = "update";
			}
		} catch (Exception e) {
			e.printStackTrace();
			retFlag = "error";
		}
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(retFlag);
		} catch (Exception e) {
		}
	}

	// 获得分类详细
	public String load() {
		if (this.typeId != null && !this.typeId.equals("")) {
			this.kmType = this.kmService.getType(Integer.valueOf(this.typeId));
			if (kmType == null) {
				kmType = new KmType();
			}
			if (this.operation != null && this.operation.equals("add")) {
				kmType.setTypePid(Integer.valueOf(this.typeId));
				kmType.setTypeId(null);
				kmType.setTypeName(null);
			}
		}
		return "toView";
	}

	// 删除分类
	public void del() {
		String retFlag = "";
		try {
			this.kmService.deleteType(Integer.valueOf(this.typeId));
			retFlag = "delete";
		} catch (SubRecExistException se) {
			retFlag = "exist";
		} catch (Exception e) {
			retFlag = "error";
		}
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(retFlag);
		} catch (Exception e) {
		}
	}

	// 进入分类选择页面
	public String toMove() {
		return "toMove";
	}

	// 移动分类
	public void move() {
		String retFlag = "";
		Integer sTypeId = Integer.valueOf(this.typeId);
		Integer tTypeId = Integer.valueOf(this.typePid);
		try {
			this.kmService.moveType(sTypeId, tTypeId);
			retFlag = "success";
		} catch (Exception e) {
			retFlag = "error";
		}
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(retFlag);
		} catch (Exception e) {
		}
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getTypePid() {
		return typePid;
	}

	public void setTypePid(String typePid) {
		this.typePid = typePid;
	}

	public KmType getKmType() {
		return kmType;
	}

	public void setKmType(KmType kmType) {
		this.kmType = kmType;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
